/*
 * Copyright (c) 2020
 * User:Palo
 * File:HomeAbilitySlice.java
 * Date:2020/12/27
 */

package com.iceframework.hiapp.slice;

import com.iceframework.hiapp.ResourceTable;
import com.iceframework.hiapp.core.utils.HiPageUtils;
import com.iceframework.hiapp.core.utils.HiToastUtils;
import com.iceframework.hiapp.entity.BottomTabEntity;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;

import java.util.ArrayList;
import java.util.List;

public class HomeAbilitySlice extends AbilitySlice {

    private static final int UPPER_LIST_LEN = 5;
    private static final int LOWER_LIST_LEN = 5;
    private static final int OVERSCROLL_PERCENT = 20;
    private static final float OVERSCROLL_RATE = 1.0f;
    private static final int REMAIN_VISIBLE_PERCENT = 20;
    private static final int LIST_ITEM_HEIGHT = 65;
    private static final int TOAST_DURATION = 1000;
    private static final int TOAST_OFFSETX = 50;
    private static final int TOAST_OFFSETY = 200;
    private static final int PROFILE_SIZE = 108;
    private static final int ORIGINAL_BACKGROUND_COLOR = 211;
    private static final int BACKGROUND_COLOR = 250;
    private static final int PROFILE_TOP_MARGIN = 100;
    private static final int MARGIN_CHANGE_RATIO = 2;
    private static final int DEFAULT_ACTIVE_BOTTOM_TAB = 1;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_home);
        initScrollView();
        initBottomTab();
    }

    private void initScrollView() {
        ScrollView scrollView = (ScrollView) findComponentById(ResourceTable.Id_home_frame_scroll);

        int profileSizePx = AttrHelper.vp2px(PROFILE_SIZE, this);
        int profileTopMarginPx = AttrHelper.vp2px(PROFILE_TOP_MARGIN, this);
        if (scrollView != null) {
            scrollView.setReboundEffectParams(OVERSCROLL_PERCENT, OVERSCROLL_RATE, REMAIN_VISIBLE_PERCENT);
            scrollView.setReboundEffect(true);
            DirectionalLayout upperBackGround = (DirectionalLayout) findComponentById(ResourceTable.Id_background_top);
            ShapeElement shapeElement = new ShapeElement();
            shapeElement.setShape(ShapeElement.RECTANGLE);

            // Set Scrolled listener
            scrollView.getComponentTreeObserver().addScrolledListener(() -> {
                HiToastUtils.showWarning(this, "滚动屏幕中", 1000);

//                float curY = scrollView.getScrollValue(Component.AXIS_Y);
//                int fixedProfileSize = (int) (profileSizePx * (profileSizePx - curY)) / profileSizePx;
//
//                if (fixedProfileSize > profileSizePx) {
//                    curY = 0;
//                    fixedProfileSize = profileSizePx;
//                } else if (fixedProfileSize < 0) {
//                    curY = profileSizePx;
//                    fixedProfileSize = 0;
//                }
//                int colorChange = (int) ((BACKGROUND_COLOR - ORIGINAL_BACKGROUND_COLOR) * curY / profileSizePx);
//                shapeElement.setRgbColor(new RgbColor(ORIGINAL_BACKGROUND_COLOR + colorChange,
//                        ORIGINAL_BACKGROUND_COLOR + colorChange, ORIGINAL_BACKGROUND_COLOR + colorChange));
//                upperBackGround.setBackground(shapeElement);
            });
        }
    }

    /**
     * 初始化底部Tab栏
     */
    private void initBottomTab() {
        DirectionalLayout bottomTab = (DirectionalLayout) findComponentById(ResourceTable.Id_bottom_tabMenu);
        //创建容器
        DirectionalLayout showFrame = (DirectionalLayout) findComponentById(ResourceTable.Id_home_show_frame);

        List<DirectionalLayout> tabList = new ArrayList<>();

        List<BottomTabEntity> bottomTabEntityList = getBottomTabEntities();
        Text titleBarText = (Text) findComponentById(ResourceTable.Id_top_title_bar);

        for (int count = 0; count < bottomTabEntityList.size(); count++) {
            // Use LayoutScatter to convert xml file into Component instance
            DirectionalLayout tab = (DirectionalLayout) LayoutScatter.getInstance(getContext())
                    .parse(ResourceTable.Layout_layout_tab, bottomTab, false);
            Image buttonImage = (Image) tab.findComponentById(ResourceTable.Id_bottom_tab_button_image);

            if (buttonImage != null) {
                if(count == DEFAULT_ACTIVE_BOTTOM_TAB - 1){
                    buttonImage.setPixelMap(Integer.parseInt(bottomTabEntityList.get(count).getActiveIcon()));
                    titleBarText.setText(bottomTabEntityList.get(count).getName());
                } else {
                    buttonImage.setPixelMap(Integer.parseInt(bottomTabEntityList.get(count).getInactiveIcon()));
                }
            }
            Text buttonText = (Text) tab.findComponentById(ResourceTable.Id_bottom_tab_button_text);
            if (buttonText != null) {
                buttonText.setText(bottomTabEntityList.get(count).getName());
            }
            int finalCount = count;
            tab.setClickedListener(component -> {
                // Deselect all tabs in tab menu
                for (DirectionalLayout btn : tabList) {
                    ((Image) btn.findComponentById(ResourceTable.Id_bottom_tab_button_image))
                            .setPixelMap(Integer.parseInt(bottomTabEntityList.get(finalCount).getInactiveIcon()));
                }

                // Set seleted state on the clicked tab
                ((Image) component.findComponentById(ResourceTable.Id_bottom_tab_button_image))
                        .setPixelMap(Integer.parseInt(bottomTabEntityList.get(finalCount).getActiveIcon()));
                switch(bottomTabEntityList.get(finalCount).getKey()){
                    case "1":
                        HiToastUtils.showInfo(this,"您点击了按钮："+bottomTabEntityList.get(finalCount).getName(),3000);
                        titleBarText.setText(bottomTabEntityList.get(finalCount).getName());
                        // 展示数据库页面
                        DirectionalLayout abilityDb = (DirectionalLayout) LayoutScatter.getInstance(getContext())
                                .parse(ResourceTable.Layout_ability_db, showFrame, false);
                        showFrame.addComponent(abilityDb);
                        break;
                    case "2":
                        HiToastUtils.showWarning(this,"您点击了按钮："+bottomTabEntityList.get(finalCount).getName(),3000);
                        titleBarText.setText(bottomTabEntityList.get(finalCount).getName());
                        try {
                            HiPageUtils.persentForResultSlience(this, DBAbilitySlice.class,"01");
                        } catch (InstantiationException e) {
                            e.printStackTrace();
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        }
                        break;
                    case "3":
                        HiToastUtils.showError(this,"您点击了按钮："+bottomTabEntityList.get(finalCount).getName(),3000);
                        titleBarText.setText(bottomTabEntityList.get(finalCount).getName());
                        try {
                            HiPageUtils.persentForResultSlience(this, FormAbilitySlice.class,"01");
                        } catch (InstantiationException e) {
                            e.printStackTrace();
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        }
                        break;
                }
            });
            tabList.add(tab);
            bottomTab.addComponent(tab);
        }
    }

    /**
     * 获取tab对象数组
     * @return
     */
    private List<BottomTabEntity> getBottomTabEntities() {
        List<BottomTabEntity> bottomTabEntityList = new ArrayList<>();
        BottomTabEntity bottomTabEntity1 = new BottomTabEntity();
        bottomTabEntity1.setKey("1");
        bottomTabEntity1.setName("首页");
        bottomTabEntity1.setActiveIcon(String.valueOf(ResourceTable.Media_icon_actived));
        bottomTabEntity1.setInactiveIcon(String.valueOf(ResourceTable.Media_icon_normal));
        bottomTabEntityList.add(bottomTabEntity1);
        BottomTabEntity bottomTabEntity2 = new BottomTabEntity();
        bottomTabEntity2.setKey("2");
        bottomTabEntity2.setName("案例");
        bottomTabEntity2.setActiveIcon(String.valueOf(ResourceTable.Media_icon_actived));
        bottomTabEntity2.setInactiveIcon(String.valueOf(ResourceTable.Media_icon_normal));
        bottomTabEntityList.add(bottomTabEntity2);
        BottomTabEntity bottomTabEntity3 = new BottomTabEntity();
        bottomTabEntity3.setKey("3");
        bottomTabEntity3.setName("关于");
        bottomTabEntity3.setActiveIcon(String.valueOf(ResourceTable.Media_icon_actived));
        bottomTabEntity3.setInactiveIcon(String.valueOf(ResourceTable.Media_icon_normal));
        bottomTabEntityList.add(bottomTabEntity3);
        return bottomTabEntityList;
    }

}
